package com.un.ebs.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.un.ebs.core.exception.BusinessException;
import com.un.ebs.core.util.SecurityUtils;
import com.un.ebs.product.domain.ProductSku;
import com.un.ebs.product.domain.ProductSkuExt;
import com.un.ebs.product.domain.ProductSkuFill;
import com.un.ebs.product.domain.ProductSkuFillExt;
import com.un.ebs.product.dto.ProductSkuFillAddDto;
import com.un.ebs.product.dto.ProductSkuFillUpdateDto;
import com.un.ebs.product.enums.ProductUnitTypeEnum;
import com.un.ebs.product.mapper.ProductSkuFillExtMapper;
import com.un.ebs.product.mapper.ProductSkuFillMapper;
import com.un.ebs.product.service.ProductSkuFillService;
import com.un.ebs.product.service.ProductSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author liubo
 * @email liubocs@163.com
 * @date 2020/2/19 18:34
 */
@Service
public class ProductSkuFillServiceImpl extends ServiceImpl<ProductSkuFillMapper, ProductSkuFill> implements ProductSkuFillService {

    @Autowired
    private ProductSkuFillExtMapper productSkuFillExtMapper;

    @Autowired
    private ProductSkuService skuService;

    @Override
    public String add(ProductSkuFillAddDto req) {
        checkSku(req.getSkuId());
        ProductSkuFill obj = new ProductSkuFill();
        obj.setSkuId(req.getSkuId());
        obj.setQty(req.getQty());
        obj.setRemark(req.getRemark());
        obj.setFillStatus(false);
        obj.insert();
        return obj.getId();
    }

    @Override
    public void update(ProductSkuFillUpdateDto req) {
        ProductSkuFill obj = baseMapper.selectById(req.getId());
        if (obj.getFillStatus()) {
            throw new BusinessException("此笔投放记录已提交并记账，不能修改。");
        }
        obj.setSkuId(req.getSkuId());
        obj.setQty(req.getQty());
        obj.setRemark(req.getRemark());
        obj.setFillStatus(false);
        obj.updateById();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void submit(String id) {
        ProductSkuFill obj = baseMapper.selectById(id);
        if (obj.getFillStatus()) {
            throw new BusinessException("此笔投放记录已提交并记账，不能重复操作。");
        }
        checkSku(obj.getSkuId());

        obj.setFillUserId(SecurityUtils.getUserContent().getId());
        obj.setFillPersonnelName(SecurityUtils.getUserContent().getPersonnelName());
        obj.setFillTime(new Date());
        obj.setFillStatus(false);
        obj.updateById();

        ProductSku sku = skuService.queryById(obj.getSkuId());
        sku.setQty(sku.getQty() + obj.getQty());
        sku.updateById();
    }

    @Override
    public void delete(String id) {
        ProductSkuFill obj = baseMapper.selectById(id);
        if (obj.getFillStatus()) {
            throw new BusinessException("此笔投放记录已提交并记账，不能修改。");
        }
        baseMapper.deleteById(id);
    }

    private void checkSku(String id) {
        ProductSkuExt sku = skuService.queryViewById(id);
        if (sku.getIsDelete()) {
            throw new BusinessException("此商品SKU已删除，不能执行此操作。");
        }
        if (sku.getSpuIsDelete()) {
            throw new BusinessException("此商品SPU已删除，不能执行此操作。");
        }
        if (sku.getUnitType() != ProductUnitTypeEnum.ProductQty) {
            throw new BusinessException(sku.getName() + " 的商品类别为 【" + sku.getUnitType().getDesc() + "】 ,不能执行此操作。");
        }
    }

    @Override
    public List<ProductSkuFill> query(String skuId) {
        LambdaQueryWrapper<ProductSkuFill> lq = Wrappers.lambdaQuery();
        lq.eq(ProductSkuFill::getSkuId, skuId);
        lq.eq(ProductSkuFill::getIsDelete, false);
        return baseMapper.selectList(lq);
    }

    @Override
    public ProductSkuFillExt queryViewById(String id) {
        return productSkuFillExtMapper.selectById(id);
    }

    @Override
    public List<ProductSkuFillExt> queryView(String skuId) {
        LambdaQueryWrapper<ProductSkuFillExt> lq = Wrappers.lambdaQuery();
        lq.eq(ProductSkuFillExt::getSkuId, skuId);
        lq.eq(ProductSkuFillExt::getIsDelete, false);
        return productSkuFillExtMapper.selectList(lq);
    }

    @Override
    public List<ProductSkuFillExt> queryViewBySpu(String spuId) {
        LambdaQueryWrapper<ProductSkuFillExt> lq = Wrappers.lambdaQuery();
        lq.eq(ProductSkuFillExt::getProductId, spuId);
        lq.eq(ProductSkuFillExt::getIsDelete, false);
        return productSkuFillExtMapper.selectList(lq);
    }
}
